package com.tencent.backstage.controller;
import com.tencent.backstage.configuration.SelfProperty;
import com.tencent.backstage.constant.CookiesConstant;
import com.tencent.backstage.dao.UserInfoDao;
import com.tencent.backstage.enums.ResultEnum;
import com.tencent.backstage.form.UserForm;
import com.tencent.backstage.service.AdminService;
import com.tencent.backstage.utils.CookieUtil;
import com.tencent.backstage.utils.ResultVOUtil;
import com.tencent.backstage.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.UUID;

/**
 * 注册登陆跳转页面和验证码
 * Created by 楠尼玛
 * 2018-03-31 16:20
 */
@Controller
@RequestMapping("/fireIndex")
@Slf4j
public class IndexController {
    @Autowired
    private SelfProperty selfProperty;
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserInfoDao userInfoDao;
    @PostMapping("/adminLogin")
    @ResponseBody
    public ResultVO index(@Valid UserForm userForm, BindingResult bindingResult){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        log.info("userForm={}",userForm);
        if(bindingResult.hasErrors()) {
            log.error("【login error】 userForm={}",userForm);
            return ResultVOUtil.error(ResultEnum.LOGIN_ERROR.getCode(),ResultEnum.LOGIN_ERROR.getMessage());
        }
       ResultVO resultVO = adminService.login(userForm.getUsername(),userForm.getPassword());
        if(resultVO.getCode().equals(0)){
            attributes.getRequest().getSession().setAttribute("sessionId",resultVO.getData());
        }
        return resultVO;
    }
    @GetMapping("/index")
    public String index(){
        return "index";
    }
    @GetMapping("/login")
    public String login(Model model){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        attributes.getRequest().getSession().setAttribute("sessionId",null);
        model.addAttribute("selfProperty", selfProperty.getSalt());
        return "login_two_columns";
    }
    @GetMapping("/lookAllUser")
    public String lookAllUser(){
        return "contacts";
    }
    @PostMapping("/allUser")
    @ResponseBody
    public ResultVO  getAllUser(){
        return adminService.getAllUser();
    }
    @PostMapping("/UserFindByOpenId")
    @ResponseBody
    public ResultVO  UserFindByOpenId(@RequestParam("openid") String openid){
        return ResultVOUtil.success(userInfoDao.findByOpenid(openid));
    }
    @GetMapping("/basic_gallery")
    public String carousel(){
        return "basic_gallery";
    }
    @GetMapping("/pin_board")
    public String notice(){
        return "pin_board";
    }
}